﻿@model ValueTuple<ItemModel, DataField>
@using NewLife;
@using NewLife.Cube
@using XCode;
@using XCode.Configuration;
@{
    var item1 = Model.Item1;
    var item2 = Model.Item2;
    var name = item1.Name;
    var value = item1.Value;
    var type = item1.Type;
    var length = item1.Length;
    var display = item2?.DisplayName;

    var atts = HtmlHelper.AnonymousObjectToHtmlAttributes(item1.HtmlAttributes);
    if (!atts.ContainsKey("class")) atts.Add("class", "layui-input");
    if (!atts.ContainsKey("placeholder")) atts.Add("placeholder", "请输入" + display);
    if (!atts.ContainsKey("maxlength")) atts.Add("maxlength", length);
    if (!atts.ContainsKey("lay_verify") && !item2.Name.EqualIgnoreCase("CreateUserID", "CreateUser", "CreateTime", "CreateIP",
                "UpdateUserID", "UpdateUser", "UpdateTime", "UpdateIP", "StateCode", "StatusCode"))
    {
        var otherRequired = "";
        var onkeyupStr = "";
        var requiredLay = !item2.Nullable ? "required" : "";
        if (display.Contains("身份证"))
        {
            otherRequired = "identity";
            onkeyupStr = @"value=value.replace(/[^\w]/g,'')";
        }
        else if (display.Contains("网址"))
            otherRequired = "url";
        else if (display.Contains("邮箱"))
            otherRequired = "email";
        else if (display.Contains("手机") || display.Contains("电话"))
        {
            otherRequired = "phone";
            onkeyupStr = @"value=value.replace(/[^\d]/g,'')";
        }
        else if (display.Contains("价格") || display.Contains("单价"))
        {
            otherRequired = "number";
            onkeyupStr = @"value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')";
        }

        if (!item2.Nullable)
        {
            var lastStr = !string.IsNullOrEmpty(otherRequired) ? "|" + otherRequired : otherRequired;
            atts.Add("lay-verify", $"required{lastStr}");

            if (!atts.ContainsKey("lay-reqtext")) atts.Add("lay-reqtext", "请输入" + display);
        }
        else
        {
            //if (!string.IsNullOrEmpty(otherRequired))
            //    atts.Add("lay-verify",otherRequired);
            if (!string.IsNullOrEmpty(onkeyupStr))
                if (!atts.ContainsKey("onkeyup")) atts.Add("onkeyup", onkeyupStr);
        }
    }
}
@if (name.EqualIgnoreCase("Pass", "Password"))
{
    if (!atts.ContainsKey("autocomplete")) atts.Add("autocomplete", "off");
    @Html.Password(name, value, atts)
}
else if (display.Contains("电话") || display.Contains("传真") || display.Contains("手机"))
{
    if (!atts.ContainsKey("type")) atts.Add("type", "tel");
    @Html.TextBox(name, value, atts)
}
else if (display.Contains("身份证"))
{
    if (!atts.ContainsKey("type")) atts.Add("type", "tel");
    @Html.TextBox(name, value, atts)
}
else if (name.EqualIgnoreCase("Phone", "TelPhone", "OfficePhone", "HomePhone", "Fax"))
{
    if (!atts.ContainsKey("type")) atts.Add("type", "tel");
    @Html.TextBox(name, value, atts)
}
else if (name.EqualIgnoreCase("Mobile", "MobilePhone", "CellularPhone"))
{
    if (!atts.ContainsKey("type")) atts.Add("type", "tel");
    @Html.TextBox(name, value, atts)
}
else if (name.EqualIgnoreCase("email", "mail"))
{
    if (!atts.ContainsKey("type")) atts.Add("type", "email");
    @Html.TextBox(name, value, atts)
}
else if (name.EndsWithIgnoreCase("url"))
{
    //if (!atts.ContainsKey("type")) atts.Add("type", "url");
    @Html.TextBox(name, value, atts)
}
else if (length < 0 || length >= 300 || (length >= 200 && name.EqualIgnoreCase("Remark", "Description", "Comment")))
{
    if (atts.ContainsKey("class")) atts.Remove("class");
    atts.Add("class", "layui-textarea");
    @Html.TextArea(name, (String)value, 3, 20, atts)
}
else if (name.EqualIgnoreCase("cron"))
{
    @Html.TextBox(name, value, atts)
    <script>
        layui.config({
            base: '/modules/' //静态资源所在路径
        }).use(['cron'], function () {
            var cron = layui.cron;
            cron.render({
                elem: "#@name", // 绑定元素
                done: function (cronStr) {
                    console.log(cronStr);
                }
            });
        })
    </script>
}
else
{
    @Html.TextBox(name, value, atts)
}